/*

    This file is part of Hometree.

    Hometree is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Hometree is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Hometree.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2011,2012 Roman Held

*/
/********************************************************************\
                            Includes
\********************************************************************/
#include <QDebug>
#include "htr/htrresource.h"
#include "htr/htrclient.h"

/********************************************************************\
                         Implementation
\********************************************************************/
/*-------------------------------------------------------------------
*/
HtrResource::HtrResource(Htr::ResourceType type, QObject *parent)
  : QObject(parent)
{
  mType   = type;
  mClient = NULL;
  mId     = 0;
  mState  = Htr::Res::StUndefined;
}

/*-------------------------------------------------------------------
*/
HtrResource::~HtrResource()
{
}

/*-------------------------------------------------------------------
*/
void HtrResource::init(HtrClient *client, const QString &url)
{
  Q_ASSERT(client);
  mClient = client;
  mUrl    = url;

  connect(mClient,SIGNAL(stateChanged()),
          this, SLOT(clientStateChanged()));
  connect(mClient,SIGNAL(urlIdent(QString,qint32,qint32)),
          this, SLOT(cbInit(QString,qint32,qint32)));
  connect(mClient,SIGNAL(resourceMsg(qint32, QDataStream&)),
          this, SLOT(cbMsg(qint32,QDataStream&)));

  clientStateChanged();
}

/*-------------------------------------------------------------------
*/
QString    HtrResource::url() const
{
  return mUrl;
}

/*-------------------------------------------------------------------
*/
qint32     HtrResource::id() const
{
  return mId;
}

/*-------------------------------------------------------------------
*/
HtrClient *HtrResource::client() const
{
  return mClient;
}

/*-------------------------------------------------------------------
*/
Htr::ResourceType HtrResource::type() const
{
  return mType;
}

/*-------------------------------------------------------------------
*/
Htr::Res::State HtrResource::state() const
{
  return mState;
}

/*-------------------------------------------------------------------
*/
void HtrResource::clientStateChanged()
{
}

/*-------------------------------------------------------------------
*/
void HtrResource::setState(Htr::Res::State state)
{
  if (mState != state) {
    mState = state;
    handleStateChange();
  }
}

/*-------------------------------------------------------------------
*/
void HtrResource::setupStream(QDataStream &s)
{
  Q_ASSERT(mClient);
  mClient->setupStream(s);
}

/*-------------------------------------------------------------------
*/
void  HtrResource::handleStateChange()
{
  emit stateChanged();
}

/*-------------------------------------------------------------------
*/
bool  HtrResource::handleMsg(qint32 msgId, QDataStream &/*stream*/)
{
  bool ret = false;
  switch (msgId) {
    case Htr::Net::CreatedMsg : 
       handleCreating(); 
       ret = true;
       break;
    default:
       break; // fuer den Rest der Meldungen ist die abgeleitete Klasse zustaendig..
  }
  return ret;
}

/*-------------------------------------------------------------------
*/
void  HtrResource::handleCreating()
/*
  Wird beim serverseitigen Erstellen fur die Instanz aufgerufen, welche
  das Erstellen bewirkt hat ("Open|Create"-Mode)
*/
{
  // Nothing to do.. fuer die abgeleitete Klasse...
}

/*-------------------------------------------------------------------
*/
void  HtrResource::cbInit(const QString &url, qint32 hasId, qint32 /*type*/)
{
  if (url == mUrl) {
    if (state() == Htr::Res::StInit) {
      mId = hasId;
      setState(Htr::Res::StIdentified);
    }
    else
      qWarning() << "HtrResource::cbInit unerwartet";
  }
}

/*-------------------------------------------------------------------
*/
void HtrResource::cbMsg(qint32 id, QDataStream &stream)
{
  if (id == mId) {
    qint32 msgId;
    stream >> msgId;
    handleMsg(msgId, stream);
  }
}

